﻿package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import Ball;
	
	public class Throwing extends Sprite
	{
		private var ball:Ball;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number = -0.1;
		private var gravity:Number = 0//.5;
		private var oldX:Number;
		private var oldY:Number;
		
		private var friction:Number = 0.9;
		
		
		public function Throwing()
		{
			init();
		}
		
		private function init():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;

			ball = new Ball();
			ball.x = 0;//stage.stageWidth / 2;
			ball.y = 0;//stage.stageHeight / 2;
			vx = 0;//Math.random() * 10 - 5;
			vy = 0;//-10;
			addChild(ball);
			ball.addEventListener(MouseEvent.MOUSE_DOWN, mOnMouseDown);
			
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
		}
		
		
		private function mOnEnterFrame(event:Event):void{
			//var position:Number= ((((ball.x*-1)/ball.width)*stage.stageWidth)*2);
			//tf.appendText("x:"+ball.x+"w"+ball.width+"..."+(ball.x*-1)/ball.width);
			//tf.text="x:"+ball.x+"w"+ball.width+"..."+(ball.x*-1)/ball.width;
			//var position:Number= ((((ball.x*-1)/ball.width)*stage.stageWidth));
			
			
			
			
			//vy += gravity;
			//vy=vy*0.9;
			vx=vx*0.9;
			ball.x += vx;
			ball.y += vy;
			
			var left:Number = 0;
			var right:Number = stage.stageWidth;
			var top:Number = 0;
			var bottom:Number = stage.stageHeight;
			
			if(ball.x> left){//if(ball.x + ball.width > right){
				
				ball.x = left;//ball.x = right - ball.width;
				vx =bounce*vx;
			}
			
			else if(ball.x <  (ball.width-stage.stageWidth)*-1){//else if(ball.x - ball.width < left){
					
				ball.x = (ball.width - stage.stageWidth)*-1;// (ball.x- ball.width) ;//ball.x = left + ball.width;
				vx *= bounce;
			}
			
			if(ball.y  < 0){
				
				//ball.y = -10;
				//vy *= bounce;
				vy=((ball.y-0)*-0.6);
			}
			else if(ball.y > 0){
				
				/*ball.y = 0;
				vy *= bounce;*/
				vy=((ball.y-0)*-0.6);
			}
		}
		
		private function mOnMouseDown(event:MouseEvent):void{
			
			
			oldX = ball.x;
			oldY = ball.y;
			
			stage.addEventListener(MouseEvent.MOUSE_UP, mOnMouseUp);
//			e((ball.width-stage.stageWidth*-1) ,0,ball.width-stage.stageWidth,0);
			var vx:Number = (ball.width - stage.stageWidth)*-1;
			var vy:Number =0 ;
			var vw:Number = ball.width - stage.stageWidth;
			var vh:Number = 0;
			//tf.appendText(":"+","+vx+","+vy+","+vw+","+vh);
			//tf.text=":"+","+vx+","+vy+","+vw+","+vh;
			var bounds:Rectangle = new Rectangle(vx,vy,vw,vh);
			ball.startDrag(false, bounds);
			removeEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			addEventListener(Event.ENTER_FRAME, trackVelocity);
		}
		
		private function mOnMouseUp(event:MouseEvent):void{
			//tf.text="mOnMouseUp";
			stage.removeEventListener(MouseEvent.MOUSE_UP, mOnMouseUp);
			ball.stopDrag();
			removeEventListener(Event.ENTER_FRAME, trackVelocity);
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
		}
		
		private function trackVelocity(event:Event):void{
			vx = (ball.x - oldX);
			vy = (ball.y - oldY);
			oldX = ball.x;
			oldY = ball.y;
		}
			}
}
